package com.ewhale.feitengguest.ui.user.mine

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.databinding.ItemInviteBinding
import com.ewhale.feitengguest.dto.ArticleDetailDto
import com.ewhale.feitengguest.dto.BalanceDto
import com.ewhale.feitengguest.dto.RankDto
import com.ewhale.feitengguest.presenter.user.mine.InvitePresenter
import com.ewhale.feitengguest.ui.WebViewActivity
import com.ewhale.feitengguest.view.user.mine.InviteView
import com.simga.library.activity.MBaseActivity
import com.simga.library.adapter.BindRecycAdapter
import com.simga.library.utils.glide.GlideUtil
import kotlinx.android.synthetic.main.activity_invite_list.*

/**
 * 作者：SimGa
 * 日期：2019/12/25
 * 时间：16:14
 * 包名：com.ewhale.feitengguest.ui.user.mine
 */
class InviteListActivity : MBaseActivity<InvitePresenter>(), InviteView {
    override fun showBalance(result: BalanceDto?) {}
    override fun isUseBaseTitleBar(): Boolean {
        return true
    }

    override fun getViewId(): Int {
        return R.layout.activity_invite_list
    }

    private val mInviteAdapter by lazy { Invitedapter() }

    override fun init(savedInstanceState: Bundle?) {
        mRlTitleBar.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color))
        mLine.visibility = View.INVISIBLE
        mTvRight.setTextColor(Color.WHITE)
        mIvBack.setImageResource(R.mipmap.d2_nav_icon_back_white)
        setRightText("活动规则")

        lv_invite.apply {
            adapter = mInviteAdapter
            layoutManager = object : LinearLayoutManager(mContext, RecyclerView.VERTICAL, false) {
                override fun canScrollVertically() = false
            }
            isNestedScrollingEnabled = false
            setHasFixedSize(false)
            isFocusable = false
        }
        presenter.getRanking()
    }

    override fun initListener() {
        super.initListener()
        mTvRight.setOnClickListener {
            presenter.loadArticleDetails(12)
        }
        btn_last.setOnClickListener {
            presenter.getLastRanking()
        }
    }

    override fun showLastRanking(result: String?) {
        WebViewActivity.open(mContext, "榜单详情", result)
    }

    override fun showRanking(list: RankDto?) {
        mInviteAdapter.setNewData(list!!.rankingDto)
        GlideUtil.loadPicture(list.poster, iv_cover)
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String?, message: String?) {
        showErrorMsg(code, message)
    }

    override fun showDetail(result: ArticleDetailDto?) {
        WebViewActivity.open(mContext, "活动规则", result!!.content)
    }

    internal class Invitedapter : BindRecycAdapter<ItemInviteBinding, RankDto.RankingDtoBean>() {
        override fun normalViewId(): Int {
            return R.layout.item_invite
        }

        override fun onBindViewHolder(mBinding: ItemInviteBinding, itemData: RankDto.RankingDtoBean, holder: RecyclerView.ViewHolder, position: Int) {
            mBinding.itemData = itemData
            mBinding.position = position
            mBinding.ivTitleIcon.setImageResource(when (position) {
                0 -> R.mipmap.no1
                1 -> R.mipmap.no2
                2 -> R.mipmap.no3
                else -> R.mipmap.no1
            })
        }
    }

    companion object {
        @JvmStatic
        fun open(mContext: MBaseActivity<*>) {
            mContext.startActivity(null, InviteListActivity::class.java)
        }
    }
}
